<template>
  <div class="home-page">
    <section class="py-5 text-center container">
      <div class="row py-lg-5">
        <div class="col-lg-6 col-md-8 mx-auto">
          <img src="../assets/callout.svg" alt="callout" class="w-50" />
          <h2 class="font-weight-light">随心写作，自由表达</h2>
          <p>
            <router-link :to="{name: 'create'}" class="btn btn-primary my-2">开始写文章</router-link>
          </p>
        </div>
      </div>
    </section>
    <h4 class="font-weight-bold text-center">发现精彩</h4>
    <ColumnList :column="testData"></ColumnList>
    <button class="btn btn-outline-primary mt-2 mb-5 mx-auto btn-block w-25 d-block" @click="loadMorePage"
      v-if="!isLastPage">
      加载更多
    </button>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, computed } from 'vue';
// import { testData } from '../../testData'
import { useStore } from 'vuex';
import { GlobalDataProps } from '../store/store';
import ColumnList from '../components/ColumnList.vue'
import useLoadMore from '../hooks/useLoadMore';

onMounted(() => {
  // 响应式的 页数，如果有就是当前页码访问，如果不是 就代表是第一页， 之后在 stort里判断是不是当前页，达到重复发送请求数据
  store.dispatch('getColumns', { currentPage: currentPage.value ? currentPage.value : 1, pageSize: 3 }) // 显示三个
})
const store = useStore<GlobalDataProps>()
const testData = computed(() => store.getters.getColumns)
// 总的页数
const total = computed(() => store.state.column.total)
const currentPage = computed(() => store.state.column.currentPage)
const { loadMorePage, isLastPage } = useLoadMore('getColumns', total, { currentPage: (currentPage.value ? currentPage.value + 1 : 2), pageSize: 3 })

</script>